package com.googlecode.flealancemarket.tapestry.pages.inbox;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.PrivateMessage;
import com.googlecode.flealancemarket.service.commands.DeleteMessage;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.tapestry.annotations.OnEvent;
import org.apache.tapestry.ioc.annotations.Inject;

public abstract class AbstractMessagesPage extends AbstractInboxPage {

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.DeleteMessage")
    private DeleteMessage deleteMessage;

    private int messagesNumber;
    private int pageNumber = 1;
    private List<PrivateMessage> messages;
    private PrivateMessage message;

    @Inject
    @SpringBean("java.util.List")
    private List<Integer> paginationNumbers;
    private Integer tempPagination;

    Object onActivate(Object[] params){
        super.onActivate();

        if(params.length==0)
            setPageNumber(1);
        else
            setPageNumber(Integer.parseInt((String)params[0]));

        messagesNumber = calculateMessagesNumber();

        messages = receiveMessagesList();

        int total = getTotalPages();

        paginationNumbers.clear();
        for(int i = 1; i <= total; i++){
            paginationNumbers.add(new Integer(i));
        }
        return null;
    }

    public abstract int calculateMessagesNumber();
    public abstract List<PrivateMessage> receiveMessagesList();

    public int getMessagesNumber() {
        return messagesNumber;
    }

    public void setMessagesNumber(int messagesNumber) {
        this.messagesNumber = messagesNumber;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public List<PrivateMessage> getMessages() {
        return messages;
    }

    public void setMessages(List<PrivateMessage> messages) {
        this.messages = messages;
    }

    public int getTotalPages(){
        return (int) Math.ceil(messagesNumber * 1.0 / 10);
    }

    public boolean getNeedPagination(){
        return getTotalPages() > 1 || getPageNumber() < getTotalPages();
    }

    public boolean getIsCurrentPage(){
        return tempPagination == pageNumber;
    }

    public List<Integer> getPaginationNumbers() {
        return paginationNumbers;
    }

    public void setPaginationNumbers(List<Integer> paginationNumbers) {
        this.paginationNumbers = paginationNumbers;
    }

    public Integer getTempPagination() {
        return tempPagination;
    }

    public void setTempPagination(Integer tempPagination) {
        this.tempPagination = tempPagination;
    }
    public void setMessage(PrivateMessage message) {
        this.message = message;
    }

    public PrivateMessage getMessage(){
        return message;
    }

    private final String format = "dd.MM.yyyy";

    public Format getFormat() {
        return new SimpleDateFormat(format);
    }

    @OnEvent(component="deleteMessage")
    void deleteMessage(Long id){
        for(PrivateMessage temp : messages){
            if(temp.get_id().equals(id)){
                deleteMessage.setMessage(temp);
                break;
            }
        }
        deleteMessage.execute();
    }
}
